package com.xinzhi.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.xinzhi.mapper.AppraisalMapper;
import com.xinzhi.pojo.Appraisal;
import com.xinzhi.service.AppraisalService;
import com.xinzhi.utils.RedisUtils;
import com.xinzhi.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AppraisalServiceImpl implements AppraisalService {
    @Autowired
    private AppraisalMapper appraisalMapper;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public Result getAppraisalByObject(String object) {

        //先去Redis中查询数据
        String userlist = (String) redisUtils.get("userlist");
        //进行Redis判断
        if (StrUtil.isNotEmpty(userlist)){
            return Result.ok(userlist);
        }

        //对参数进行判断
        if (object ==null){
            return Result.error("查询标题不能为空");
        }
        //进行数据库查询
        List<Appraisal> appraisalByObject = appraisalMapper.getAppraisalByObject(object);
        if (appraisalByObject !=null){
            redisUtils.set("userlist", JSONUtil.toJsonStr(appraisalByObject));
            return Result.ok(appraisalByObject);
        }else {
            return Result.error("数据库中没有这条数据");
        }
    }
}
